package com.qc.wintrax.utils;

import android.util.Log;

/**
 * 
 * @ClassName: LogUtil
 * @Description: TODO(日志工具类)
 * @author fujianxiong
 * @date 2015年8月8日 上午2:57:40
 *
 */
public abstract class LogUtil {
	public static boolean allowD = true;
	public static boolean allowE = true;
	public static boolean allowI = true;
	public static boolean allowV = true;
	public static boolean allowW = true;
	public static boolean allowWtf = true;

	/**
	 * 
	* @Title: d
	* @Description: TODO(d方法)
	* @author fujianxiong
	* @param content
	* @throws
	 */
	public static void d(String content) {
		if (!allowD) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.d(tag, content);
	}

	public static void d(String content, Throwable tr) {
		if (!allowD) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.d(tag, content, tr);
	}

	/**
	 * 
	* @Title: e
	* @Description: TODO(e方法)
	* @author fujianxiong
	* @param content
	* @throws
	 */
	public static void e(String content) {
		if (!allowE) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.e(tag, content);
	}

	public static void e(String content, Throwable tr) {
		if (!allowE) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.e(tag, content, tr);
	}

	/**
	 * 
	* @Title: i
	* @Description: TODO(i方法)
	* @author fujianxiong
	* @param content
	* @throws
	 */
	public static void i(String content) {
		if (!allowI) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.i(tag, content);
	}

	public static void i(String content, Throwable tr) {
		if (!allowI) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.i(tag, content, tr);
	}

	/**
	 * 
	* @Title: v
	* @Description: TODO(v方法)
	* @author fujianxiong
	* @param content
	* @throws
	 */
	public static void v(String content) {
		if (!allowV) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.v(tag, content);
	}

	public static void v(String content, Throwable tr) {
		if (!allowV) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.v(tag, content, tr);
	}

	/**
	 * 
	* @Title: w
	* @Description: TODO(w方法)
	* @author fujianxiong
	* @param content
	* @throws
	 */
	public static void w(String content) {
		if (!allowW) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.w(tag, content);
	}

	public static void w(String content, Throwable tr) {
		if (!allowW) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.w(tag, content, tr);
	}

	public static void w(Throwable tr) {
		if (!allowW) {
			return;
		}
		StackTraceElement caller = getCallerMethodName();
		String tag = generateTag(caller);
		Log.w(tag, tr);
	}

	/**
	 * 
	* @Title: getCallerMethodName
	* @Description: TODO(跟踪到调用该日志的方法)
	* @author fujianxiong
	* @return
	* @throws
	 */
	private static StackTraceElement getCallerMethodName() {
		StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
		return stacks[4];
	}

	/**
	 * 
	* @Title: generateTag
	* @Description: TODO(规范TAG格式：类名[方法名， 调用行数])
	* @author fujianxiong
	* @param caller
	* @return
	* @throws
	 */
	private static String generateTag(StackTraceElement caller) {
		String tag = "%s[%s, %d]";
		String callerClazzName = caller.getClassName();
		callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
		tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
		return tag;
	}

}
